package com.bos.service.impl;

import com.bos.data.model.*;
import com.bos.data.repositories.ProjectRepository;
import com.bos.data.repositories.jpa.BosLogJPARepository;
import com.bos.data.repositories.jpa.BosProjectJPARepository;
import com.bos.data.repositories.jpa.ProjectHistoryJPARepository;
import com.bos.mapper.ProjectMapper;
import com.bos.service.ProjectService;
import com.bos.common.PageData;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service("projectServiceImpl")
public class ProjectServiceImpl implements ProjectService {
    private Logger logger = LoggerFactory.getLogger(ProjectServiceImpl.class);
    @Autowired
    ProjectMapper proMapper;

    //1.查询project表中所有的数据
    @Autowired
    private BosProjectJPARepository bosProjectJPARepository;

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private ProjectHistoryJPARepository projectHistoryJPARepository;

    @Autowired
    private BosLogJPARepository bosLogJPARepository;

    @Transactional
    @Override
    public List<BosProjectModel> findProjects(String name) {
        return proMapper.findProjects(name);
    }

    //2.将数据插入Project表中
    @Transactional
    @Override
    public int insertProject(BosProjectModel pro) {
        return proMapper.insertProject(pro);
    }

    //3.如果没有输入结束时间的话将数据插入Project表中
    @Transactional
    @Override
    public int insertProject2(BosProjectModel pro) {
        return proMapper.insertProject2(pro);
    }

    //4.根据ID修改有完成时间的数据
    @Transactional
    @Override
    public int updateById(BosProjectModel pro) {
        // TODO Auto-generated method stub
        return proMapper.updateById(pro);
    }

    //5.根据ID修改有完成时间的数据
    @Override
    public int updateById2(BosProjectModel pro) {
        // TODO Auto-generated method stub
        return proMapper.updateById2(pro);
    }

    //6.根据ID获取当前点击的项目的数据
    @Transactional
    @Override
    public BosProjectModel findProjectById(int id) {
        // TODO Auto-generated method stub
        return proMapper.findProjectById(id);
    }

    @Override
    public int updateById3(BosProjectModel pro) {
        // TODO Auto-generated method stub
        return proMapper.updateById3(pro);
    }


    @Transactional
    //11.根据id删除对应的数据
    @Override
    public int deleteById(int id) {
        return proMapper.deleteById(id);
    }

    @Override
    public List<BosProjectModel> findProject() {
        // TODO Auto-generated method stub
        return proMapper.findProject();
    }

    //查询出用户表中的数据
    @Override
    public List<BosUserModel> findUsers() {
        return proMapper.findUsers();
    }

    //13.修改bos_project中的leader
    @Override
    public int updateLeader(BosProjectModel project) {
        return proMapper.updateLeader(project);
    }

    @Override
    public BosCustomerModel findCustomerByCname(String cname) {
        return proMapper.findCustomerByCname(cname);
    }

    //15.根据typeName为项目进度查询出数据
    @Override
    public List<BosParamModel> findBos_params(String typeName) {
        return proMapper.findBos_params(typeName);
    }

    //10.根据多个条件查询出结果
    @Override
    public List<BosProjectModel> selectByWhere(String where, String[] shuigou,
                                               String[] wtype, String[] pteam, String[] pleader) {
        // TODO Auto-generated method stub
        return proMapper.selectByWhere(where, shuigou, wtype, pteam, pleader);
    }


    @Transactional
    @Override
    public void updProject(BosProjectModel bosProjectModel) {
        try {
            bosProjectJPARepository.save(bosProjectModel);
        } catch (Exception e) {
            logger.error("更新项目失败");
        }
    }

    @Transactional
    @Override
    public int addProject(BosProjectModel bosProjectModel) {
        try {
            BosProjectModel pmodel = bosProjectJPARepository.saveAndFlush(bosProjectModel);
            return pmodel.getId();
        } catch (Exception e) {
            logger.error("添加项目失败");
            return 0;
        }
    }

    @Override
    public PageData findWithTerm(String where, List<String> shuigou, List<String> wtype, List<String> pteam, List<String> pleader, int pageSize, int pageIndex) {
        try {
            PageData data = projectRepository.findWithTerm(where, shuigou, wtype, pteam, pleader, pageSize, pageIndex);
            return data;
        } catch (Exception e) {
            logger.error("高级搜索失败:[where:{},shuigou:{},wtype:{},pteam:{},pleader:{}]", where, shuigou, wtype, pteam, pleader);
        }
        return null;
    }

    @Transactional
    @Override
    public int addProjectHistory(TProjectHistoryModel model){
        try {
            return projectHistoryJPARepository.save(model).getId();
        }catch (Exception e){
            logger.error("新增项目历史失败",e);
            return 0;
        }
    }

    @Override
    public List<TProjectHistoryModel> getAllProjectHistoryByWName(String name){
        return projectHistoryJPARepository.getAllByWName(name);
    }

    @Override
    public BosProjectModel findByName(String name){
        return bosProjectJPARepository.findByWname(name);
    }
    //16.查询出去除重复数据的项目负责人
    @Override
    public List<BosProjectModel> findLeaders(){
        return  proMapper.findLeaders();
    }

    //复制一条项目数据
    @Transactional
    @Override
    public String copyProject(int id){
        try {
            BosProjectModel project=bosProjectJPARepository.findOne(id);
            BosProjectModel newProject=new BosProjectModel();
            BeanUtils.copyProperties(project,newProject);
            newProject.setId(null);
            newProject.setwName("待重命名_"+project.getwName());
            newProject.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            bosProjectJPARepository.save(newProject);
            //复制一份到项目历史里
            TProjectHistoryModel historyModel=new TProjectHistoryModel();
            BeanUtils.copyProperties(newProject,historyModel);
            projectHistoryJPARepository.save(historyModel);
            return project.getwName();
        }catch (Exception e){
            logger.error("copy项目失败:pid:{}",id,e);
            return null;
        }
    }

    @Override
    public LogsModel findLogById(int id){
        LogsModel model=null;
        try {
            model=bosLogJPARepository.findOne(id);
            return model;
        }catch (Exception e){
            logger.error("根据日志id查数据失败",e);
            return model;
        }
    }
}
